﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Globalization;
using ResourceBlender.Core;

/// <summary>
/// Miscellaneuous methods
/// </summary>
public class ASPNETUtils {
    /// <summary>
    /// Gets a message with instructions for the user to copy and paste sections into the web.config when ASP.NET cannot write to it automatically.
    /// </summary>
    /// <param name="salt">The salt.</param>
    /// <returns></returns>
    public static string GetWebConfig(Dictionary<string, string> appSettings, string connectionString, string providerName) {
        StringBuilder buffer = new StringBuilder(I18nUtils.GetString("Errors", "ManualWebConfig"));
        buffer.Append("</p><p><strong>");
        if(appSettings != null) {
            buffer.Append("&lt;appSettings&gt;<br />&lt;clear /&gt;<br />");
            string fmt = "&lt;add key=&quot;{0}&quot; value=&quot;{1}&quot; /&gt;<br />";
            foreach(KeyValuePair<string, string> setting in appSettings) {
                buffer.AppendFormat(fmt, setting.Key, setting.Value);
            }
            buffer.Append("&lt;/appSettings&gt;<br />");
        }
        if(connectionString != null) {
            buffer.Append("&lt;connectionStrings&gt;<br />&lt;clear /&gt;<br />");

            buffer.Append(string.Format("&lt;add name=&quot;Default&quot; connectionString=&quot;{0}&quot; providerName=&quot;{1}&quot; /&gt;<br />", connectionString, providerName));
            buffer.Append("&lt;/connectionStrings&gt;");
        }
        buffer.Append("</strong>");
        return buffer.ToString();
    }


    /// <summary>
    /// Assigns a jQuery click/ajax listener to a 'Find Similar' button.
    /// </summary>
    /// <param name="buffer">The buffer containing the rest of the JavaScript.</param>
    /// <param name="buttonId">The client ID of the 'find translaiton' button.</param>
    /// <param name="textboxId">The client ID of the textbox containing the search string.</param>
    /// <param name="containerId">The client ID of the container for the results.</param>
    /// <param name="languageCode">The language code of the translation.</param>
    /// <param name="addFormResource"><c>true</c> if this button is on the 'add resource' form.</param>
    public static void AssignFindSimilarListener(StringBuilder buffer, string buttonId, string textboxId, string containerId, string languageCode, bool addFormResource) {
        buffer.AppendFormat("addFindSimilarListener('{0}', '{1}', '{2}', '{3}', {4});\n", buttonId, textboxId, containerId, languageCode, addFormResource ? "true" : "false");
    }

    /// <summary>
    /// Assigns a jQuery click/ajax listener to a 'Translate with Google' button.
    /// </summary>
    /// <param name="buffer">The buffer containing the rest of the JavaScript.</param>
    /// <param name="buttonId">The client ID of the 'find translaiton' button.</param>
    /// <param name="srcTextboxId">The client ID of the textbox containing the string to translate.</param>
    /// <param name="srcGoogleCode">The Google code of the source textbox.</param>
    /// <param name="destTextboxId">The client ID of the textbox in which to display the translation.</param>
    /// <param name="destGoogleCode">The Google code of the destination textbox.</param>
    public static void AssignGoogleTranslateSimilarListener(StringBuilder buffer, string buttonId, string srcTextboxId, string srcGoogleCode, string destTextboxId, string destGoogleCode) {
        buffer.AppendFormat("addGoogleTranslateListener('{0}', '{1}', '{2}', '{3}', '{4}');\n", buttonId, srcTextboxId, srcGoogleCode, destTextboxId, destGoogleCode);
    }

    /// <summary>
    /// Assigns a jQuery click listener to open the PO editor.
    /// </summary>
    /// <param name="buffer">The buffer containing the rest of the JavaScript.</param>
    /// <param name="buttonId">The ID of the button to which to assign the listener.</param>
    /// <param name="dataTextboxId">The ID of the textbox containing the PO metadata.</param>
    public static void AssignEditPOListener(StringBuilder buffer, string buttonId, string dataTextboxId) {
        buffer.AppendFormat("$('#{0}').click(function() {{ editPO('{1}'); return false; }});\n", buttonId, dataTextboxId);
    }


    /// <summary>
    /// Formats a localized string containing {linkstart} and {linkend} tags, then returns a hyperlink to <c>linkURL</c>.
    /// </summary>
    /// <param name="format">The string format.</param>
    /// <param name="linkUrl">The link URL.</param>
    /// <returns>The localized string containing a link</returns>
    public static string FormatLink(string format, string linkUrl) {
        if(string.IsNullOrEmpty(format) || string.IsNullOrEmpty(linkUrl)) {
            return string.Empty;
        }
        return format.Replace("{linkstart}", string.Format("<a href=\"{0}\">", linkUrl)).Replace("{linkend}", "</a>");
    }

    /*public static string GetString(string bundle, string key) {
        return HttpContext.GetGlobalResourceObject(bundle, key, CultureInfo.CurrentUICulture).ToString();
    }*/

}
